<template>
    <view class="micro-temp">
        <!-- #ifdef APP-PLUS || MP-WEIXIN-->
        <web-view
            class="webView"
            :webview-styles="webviewStyles"
            ref="webView"
            :src="url"
            @message="reciveMessage"></web-view>
        <!-- #endif -->
        <!-- #ifdef H5 -->
        <iframe class="main" :height="listHeight" id="iframeContent" :src="url" />
        <!-- #endif -->
    </view>
</template>

<script>
import { mapMutations, mapState } from "vuex"
import storage from "@/uni-sub-base/utils/storage.js"
import { getUserCarList } from "@/utils/cartUtil.js"
import { getUserInfo, getProductDisplay } from "@/utils/appInit.js"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
export default {
    props: {
        url: {
            type: String,
            default: "",
        },
    },
    data() {
        return {
            listHeight: 0,
            history: 0,
            webviewStyles: {
                progress: false,
            },
        }
    },
    mounted() {
        //#ifdef H5
        this.getListHeight()
        //#endif
        // #ifdef APP-PLUS
        let defaultSite = storage.getCache("DefaultSite")
        // if(defaultSite && uni.getSystemInfoSync().platform=='ios'){
        const currentWebview = this.getWebView()
        let that = this
        setTimeout(() => {
            uni.getSystemInfo({
                //调用uni-app接口获取屏幕高度
                success(res) {
                    //成功回调函数
                    let statusBarHeight = res.statusBarHeight
                    let ph = res.windowHeight
                    let height = ph - statusBarHeight
                    currentWebview.setStyle({
                        top: statusBarHeight,
                        height: height,
                    })
                },
            })
            storage.removeCache("DefaultSite")
        }, 2000) //如果是页面初始化调用时，需要延时一下
        // }

        // #endif
    },
    onLoad(e) {},
    onShow() {},
    methods: {
		...mapMutations(["SET_AUTH"]),
        //获取列表高度
        getListHeight() {
            //获取高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
            })
        },
        reciveMessage(e) {
			console.log(4234324324)
			console.log(e)
            try {
                if (e.detail.data[0]) {
                    if (e.detail.data[0].path) {
						console.log('product')
                        let path = e.detail.data[0].path
                        if (path.includes("/pages/product/index")) {
                            // let path.rep
                            let productTagId = path.split("=")[1]
                            storage.setCache("productTagId", productTagId)
                            this.$nextTick(() => {
                                this.easyGoTo("/pages/product/index")
                            })
                        } else {
                            this.easyGoTo(path)
                        }
                    } else if (e.detail.data[0].action) {
                        let action = e.detail.data[0].action
						console.log('logout')
                        if (action == "logout") {
                            //退出登录
                            storage.removeCache("token")
                            uni.reLaunch({
                                url: "/pages/login/index",
                            })
                        }
                    }
					if (e.detail.data && e.detail.data.length > 0) {
						let list = e.detail.data
						for (let i = list.length - 1; i >= 0; i--) {
							if (list[i].token) {
								console.log("设置token")
								let token = list[i].token;
								this.SET_AUTH(token)
								break
							}
						}
					}
                }
            } catch (e) {
                //TODO handle the exception
                console.log(e)
            }
        },
        getWebView() {
            return this.$parent.$scope.$getAppWebview().children()[0]
        },
    },
}
</script>

<style lang="scss">
.main {
    position: absolute;
    display: block;
    border: 0px;
    width: 100%;
}
</style>
